<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>水平居中</title>
    <style>
        /* 父子容器基础样式 */
        .father{
            width: 500px;
            height: 500px;
            background-color: green;
        }
        .son{
            width: 100px;
            height: 100px;
            background-color: red;
        }

        /* 方式一：flex盒子模型 */
        .case1{
            display:flex;
            align-items: center;
            justify-content: center;
        }

        /* 方式二：父相子绝，50%-50%子元素自身偏移修正*/
        .case2{
            position: relative;
        }
        .case2>.son{
            position: absolute;
            top: 50%;
            left: 50%;
            transform: translate(-50%,-50%);
        }

    </style>
</head>
<body>
    <p>方法一：flex盒子模型</p>
    <div class="father case1">
        <div class="son"></div>
    </div>
    <p>方法二：父相子绝+子元素50%-50%自身宽高修正</p>
    <div class="father case2">
        <div class="son"></div>
    </div>
</body>
</html>